package plugins.system;

import plugins.Plugin;
import main.Room;
import main.User;

public class PersonalMessagePlugin extends Plugin {

   @Override
   public void execute(Room room, User user, String command, String text) {
      if (text == null || text == "") return;
      
      User target = room.getUserByName(text.split(" ")[0]);
      if (target == null) return;
      
      String msg = getMsg(text);
      if (msg == null || msg == "") return;
      
      user.dispatch("[To " + target.name + "] " + msg);
      target.dispatch("[From " + user.name + "] " + msg);
   }
   
   private String getMsg(String text) {
      String[] words = text.split(" ");
      String newLine = "";
      if (words.length <= 1) {
         return "";
      }
      for (int i = 1; i < words.length; i++) {
         newLine += words[i];
         if (i+1 < words.length) {
            newLine += " ";
         }
      }
      return newLine;
   }

}
